package com.lemon.cloud.datascope.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.lemon.cloud.comm.constants.OAuth2Constant;
import com.lemon.cloud.comm.constants.enums.ApplicationEnum;
import com.lemon.cloud.comm.constants.enums.DataScopeTypeEnum;
import com.lemon.cloud.comm.po.RoleApplication;
import com.lemon.cloud.core.utils.SpringContextHolder;
import com.lemon.cloud.core.utils.WebUtils;
import com.lemon.cloud.datascope.service.DataScopeService;
import com.lemon.cloud.security.util.SecurityUtils;
import lombok.experimental.UtilityClass;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 数据权限工具类
 * @author HuangDS
 */
@UtilityClass
public class DataScopeUtils {

    /**
     * 获取当前用户的数据权限
     * @return
     */
    public List<RoleApplication> getUserDataScope(){
        List<String> roleCode = SecurityUtils.getRoleCode();
        if (CollectionUtil.isEmpty(roleCode)){
            return Lists.newArrayList();
        }
        DataScopeService scopeService = SpringContextHolder.getBean(DataScopeService.class);
        String appId = WebUtils.getAppId(OAuth2Constant.APP_ID_HEAD);
        if (StrUtil.isEmpty(appId)){
            appId = ApplicationEnum.BASE_PLATFORM.getId().toString();
        }
        return scopeService.getDSList(roleCode,appId);
    }

    /**
     * 当前用户权限是否在传入值的范围内
     * @param org
     * @return
     */
    public boolean isInDataScope(List<Long> org){
        if (CollectionUtil.isEmpty(org)){
            return true;
        }
        List<RoleApplication> userDataScope = getUserDataScope();
        if (CollectionUtil.isEmpty(userDataScope)){
            return false;
        }
        Map<Integer, List<RoleApplication>> map = userDataScope.stream().collect(Collectors.groupingBy(RoleApplication::getScopeType));
        if (CollectionUtil.isNotEmpty(map.get(DataScopeTypeEnum.ALL.getType()))){
            //查询全部
            return true;
        }else {
            HashSet<Long> dataList = Sets.newHashSet();

            List<RoleApplication> custom = map.get(DataScopeTypeEnum.CUSTOM.getType());
            if (CollectionUtil.isNotEmpty(custom)){
                custom.forEach(item->{
                    if (CollectionUtil.isNotEmpty(item.getOrgIdList())){
                        dataList.addAll(item.getOrgIdList());
                    }
                });
            }

            List<RoleApplication> ownChild = map.get(DataScopeTypeEnum.OWN_CHILD_LEVEL.getType());
            List<Long> orgIds = SecurityUtils.getUser().getOrgIds();
            if (CollectionUtil.isNotEmpty(ownChild)) {
                DataScopeService scopeService = SpringContextHolder.getBean(DataScopeService.class);
                dataList.addAll(scopeService.getDescendantList(orgIds));
            }
            List<RoleApplication> own = map.get(DataScopeTypeEnum.OWN_LEVEL.getType());
            if (CollectionUtil.isNotEmpty(own)) {
                dataList.addAll(orgIds);
            }
            return CollectionUtil.containsAll(dataList,org);
        }
    }
}
